//package com.ly.mpayment.rest.system.ele;
//
//import java.util.Calendar;
//import java.util.Date;
//import java.util.List;
//import java.util.Map;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.ws.rs.GET;
//import javax.ws.rs.POST;
//import javax.ws.rs.Path;
//import javax.ws.rs.core.Context;
//
//import org.apache.log4j.Logger;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
//import org.springframework.stereotype.Component;
//
//import com.ly.mpayment.core.util.BigUtil;
//import com.ly.mpayment.core.util.Config;
//import com.ly.mpayment.core.util.DateUtils;
//import com.ly.mpayment.core.util.JsonUtils;
//import com.ly.mpayment.core.util.MapUtil;
//import com.ly.mpayment.core.util.ResultMessage;
//import com.ly.mpayment.persist.entity.DownRechargeFlow;
//import com.ly.mpayment.persist.entity.DownShopInfo;
//import com.ly.mpayment.persist.entity.UpOrder;
//import com.ly.mpayment.persist.entity.ele.EleRecord;
//import com.ly.mpayment.persist.service.downrechargeflow.IDownRechargeFlowService;
//import com.ly.mpayment.persist.service.downshopinfo.IDownShopInfoService;
//import com.ly.mpayment.persist.service.ele.IEleRecordService;
//import com.ly.mpayment.persist.service.uporder.IUporderService;
//import com.ly.mpayment.persist.service.user.IUserService;
//import com.ly.mpayment.rest.AbsRest;
//import com.ly.mpayment.rest.recharge.OrderProcessing;
//import com.ly.mpayment.rest.up.electronic.EleAutoCommitService;
//
//import cn.jpush.api.utils.StringUtils;
//import net.sf.json.JSONArray;
//import net.sf.json.JSONObject;
//
//@Component
//@Path("system/elerecord/")
//public class EleRecordRest extends AbsRest {
//	Logger logger = Logger.getLogger(EleRecordRest.class);
//	@Autowired
//	IUserService userService;
//	@Autowired
//	IEleRecordService eleRecordeService;
//	@Autowired
//	IDownRechargeFlowService flowService;
//	@Autowired
//	IUporderService orderService;
//	@Autowired
//	IDownShopInfoService shopService;
//	@Autowired
//	private ThreadPoolTaskExecutor taskExecutor;
//	@Autowired
//	EleAutoCommitService eleAutoService;
//
//	@SuppressWarnings("static-access")
//	@POST
//	@Path("findPageRecord")
//	public String findPageRecord(String data, @Context HttpServletRequest request) {
//		String result = this.validationParameter(data, request, userService, "start", "pageSize", "phoneNum",
//				"rechargeState", "flow", "amount", "startTime", "endTime", "accountName");
//		if (result == null) {
//			JSONObject json = JSONObject.fromObject(data);
//			Map<String, Object> parameters = MapUtil.getMap();
//
//			if (!StringUtils.isEmpty(json.getString("accountName"))) {
//				parameters.put(":accountName", json.getString("accountName"));
//			}
//			if (!StringUtils.isEmpty(json.getString("phoneNum"))) {
//				parameters.put(":phoneNum", json.getString("phoneNum"));
//			}
//			if (!StringUtils.isEmpty(json.getString("rechargeState"))) {
//				parameters.put(":rechargeState", json.getInt("rechargeState"));
//			}
//			if (!StringUtils.isEmpty(json.getString("flow"))) {
//				parameters.put(":flow", json.getString("flow"));
//			}
//			if (!StringUtils.isEmpty(json.getString("amount"))) {
//				parameters.put(":amount", BigUtil.getBigDecimal(json.getString("amount")));
//			}
//			if (!StringUtils.isEmpty(json.getString("startTime"))) {
//				parameters.put(":startTime", json.getString("startTime").concat(" 00:00:00"));
//			}
//			if (!StringUtils.isEmpty(json.getString("endTime"))) {
//				parameters.put(":endTime", json.getString("endTime").concat(" 23:59:59"));
//			}
//			return ResultMessage.getSucceed(
//					JsonUtils.excludes(
//							eleRecordeService.findPageRecord(parameters, json.getInt("start"), json.getInt("pageSize")),
//							new String[] { "eleRecords", "upOrder" }));
//		}
//		return result;
//	}
//
//	@SuppressWarnings("static-access")
//	@POST
//	@Path("failtRecord")
//	public String failtRecord(String data, @Context HttpServletRequest request) {
//		String result = this.validationParameter(data, request, userService, "rechargeState", "recordId");
//		if (result == null) {
//			JSONObject json = JSONObject.fromObject(data);
//			EleRecord ele = this.eleRecordeService.findById(json.getString("recordId"));
//			if (ele != null) {
//				logger.info("failtRecord:" + ele.getPhoneNum());
//				ele.setRechargeState(json.getInt("rechargeState"));
//				ele.setEndTime(DateUtils.getNowDate());
//				this.eleRecordeService.updateEleRecord(ele);
//				UpOrder order = this.orderService.findByEleId(ele.getUpOrder().getUporderuuid());
//				if (order != null) {
//					// 查询上游订单对应的下游订单号
//					failtOrder(order.getDownRechargeFlow().getDownorderuuid());
//					logger.info("failtRecord:" + ele.getPhoneNum());
//					return ResultMessage.getSucceed(null);
//				}
//			}
//			return ResultMessage.getFailt("");
//		}
//		return result;
//	}
//
//	@SuppressWarnings("static-access")
//	@POST
//	@Path("failtRecords")
//	public String failtRecords(String data, @Context HttpServletRequest request) {
//		String result = this.validationParameter(data, request, userService, "records");
//		if (result == null) {
//			JSONArray json = JSONObject.fromObject(data).getJSONArray("records");
//			if (json != null && json.size() > 0) {
//				for (int i = 0; i < json.size(); i++) {
//					EleRecord ele = this.eleRecordeService.findById(json.getJSONObject(i).getString("recordId"));
//					if (ele != null) {
//						logger.info("failtRecords:" + ele.getPhoneNum());
//						ele.setRechargeState(Config.rechargeState[3]);
//						ele.setEndTime(DateUtils.getNowDate());
//						this.eleRecordeService.updateEleRecord(ele);
//						UpOrder order = this.orderService.findByEleId(ele.getUpOrder().getUporderuuid());
//						if (order != null) {
//							// 查询上游订单对应的下游订单号
//							failtOrder(order.getDownRechargeFlow().getDownorderuuid());
//							logger.info("failtRecords:" + ele.getPhoneNum());
//						}
//					}
//				}
//
//				return ResultMessage.getSucceed(null);
//			}
//			return ResultMessage.getFailt("");
//		}
//		return result;
//	}
//
//	/**
//	 * 失败订单
//	 *
//	 * @param downorderuuid
//	 * @return
//	 */
//	private String failtOrder(String downorderuuid) {
//		DownRechargeFlow flow = this.flowService.findById(downorderuuid);
//		if (flow != null) {
//			UpOrder order = this.orderService.findByDownFlowId(flow.getDownorderuuid());
//			if (order != null && order.getStatus() != Config.status[2]) {
//				logger.info(
//						"failtOrder手动充值失败的订单号:".concat(flow.getDownorderuuid()) + "," + order.getUpphonenum());
////				flowService.orderHanlderFailt(order, orderService, flowService, shopService, taskExecutor,
////						Config.orderMuError);
//
//				return ResultMessage.getSucceed(null);
//			} else {
//				int tempStatus = flow.getStatus();
//				flow.setStatus(Config.status[2]);
//				flow.setStatusremark(Config.orderMuError);
//				Date end = DateUtils.getNowDate();
//				flow.setDownendtime(end);
//				flow.setDownreqnum(flow.getDownreqnum() == 0 ? 0 : flow.getDownreqnum() + 1);
//				flow.setDownendtimesec(((Long) (end.getTime() - flow.getCreatetime().getTime())).intValue());
////				List<OrderMatchDetail> details = this.orderMatchService.findNotExecuteMatch(flow.getDownorderuuid());
////				if (!details.isEmpty()) {
////					for (OrderMatchDetail de : details) {
////						de.setExecute(Config.execute);
////					}
////				}
////				DownShopInfo shop = shopService.findShopInfoByUuid(flow.getUserFlow().getUseruuid());
////				if (shop != null) {
////					// 下游订单失败 //0等待回调中 1 回调成功 2 失败 3 下游也在使用此值 4 订单发送到上游失败
////					// 5发送订单到上游成功
////					// 上游订单为空时执行
////					flowService.updateCanelOrder(flow, shop, details,tempStatus);
////					OrderProcessing.sendResultDownShop(flow, flowService, taskExecutor, shopService);
////					logger.info(
////							"failtOrder手动充值失败的订单号:".concat(flow.getDownorderuuid()) + "," + flow.getDowonphonenum());
////					return ResultMessage.getSucceed(null);
////				}
//			}
//		}
//		return null;
//	}
//
//	@SuppressWarnings("static-access")
//	@POST
//	@Path("successOrder")
//	public String successOrder(String data, @Context HttpServletRequest request) {
//		String result = this.validationParameter(data, request, userService, "recordId");
//		if (result == null) {
//			JSONObject json = JSONObject.fromObject(data);
//			EleRecord ele = this.eleRecordeService.findById(json.getString("recordId"));
//			if (ele != null) {
//				logger.info("successOrder:" + ele.getPhoneNum());
//				json.clear();
//				json.put("liushuihao","人工");
//				eleAutoService.chargeSuccess(ele,json.toString());
//				logger.info("successOrder:" + ele.getPhoneNum());
//				return ResultMessage.getSucceed(null);
//			}
//			return ResultMessage.getFailt("");
//		}
//		return result;
//	}
//
//	@SuppressWarnings("static-access")
//	@POST
//	@Path("successOrders")
//	public String successOrders(String data, @Context HttpServletRequest request) {
//		String result = this.validationParameter(data, request, userService, "records");
//		if (result == null) {
//			JSONArray json = JSONObject.fromObject(data).getJSONArray("records");
//			if (json != null && json.size() > 0) {
//				for (int i = 0; i < json.size(); i++) {
//					EleRecord ele = this.eleRecordeService.findById(json.getJSONObject(i).getString("recordId"));
//					if (ele != null) {
//						logger.info("successOrders:" + ele.getPhoneNum());
//						JSONObject liushuihao = new JSONObject();
//						liushuihao.put("liushuihao","手工单");
//						eleAutoService.chargeSuccess(ele,liushuihao.toString());
//						logger.info("successOrders:" + ele.getPhoneNum());
//					}
//				}
//				return ResultMessage.getSucceed(null);
//			}
//			return ResultMessage.getFailt("");
//		}
//		return result;
//	}
//
//	@SuppressWarnings("static-access")
//	@POST
//	@Path("againCommit")
//	public String againCommit(String data, @Context HttpServletRequest request) {
//		String result = this.validationParameter(data, request, userService, "recordId");
//		if (result == null) {
//			JSONObject json = JSONObject.fromObject(data);
//			EleRecord ele = this.eleRecordeService.findById(json.getString("recordId"));
//			if (ele != null) {
//				logger.info("againCommit:" + ele.getPhoneNum());
//				ele.setRechargeState(Config.rechargeState[4]);
//				if (eleRecordeService.updateEleRecord(ele)) {
//					logger.info("againCommit:" + ele.getPhoneNum());
//					return ResultMessage.getSucceed(null);
//				}
//			}
//			return ResultMessage.getFailt("");
//		}
//		return result;
//	}
//
//	@SuppressWarnings("static-access")
//	@POST
//	@Path("againCommits")
//	public String againCommits(String data, @Context HttpServletRequest request) {
//		String result = this.validationParameter(data, request, userService, "records");
//		if (result == null) {
//			JSONArray json = JSONObject.fromObject(data).getJSONArray("records");
//			if (json != null && json.size() > 0) {
//				for (int i = 0; i < json.size(); i++) {
//					EleRecord ele = this.eleRecordeService.findById(json.getJSONObject(i).getString("recordId"));
//					if (ele != null) {
//						logger.info("againCommits:" + ele.getPhoneNum());
//						ele.setRechargeState(Config.rechargeState[4]);
//						if (!eleRecordeService.updateEleRecord(ele)) {
//							return ResultMessage.getFailt("");
//						}
//						logger.info("againCommits:" + ele.getPhoneNum());
//					}
//				}
//				return ResultMessage.getSucceed(null);
//			}
//			return ResultMessage.getFailt("");
//		}
//		return result;
//	}
//
//	@SuppressWarnings("static-access")
//	@GET
//	@Path("updateTime")
//	public String updateTime(@Context HttpServletRequest request) {
//		List<EleRecord> eles = this.eleRecordeService.queryStatus();
//		if (eles != null &&eles.size() > 0) {
//			for (int i = 0; i < eles.size(); i++) {
//				EleRecord ele = this.eleRecordeService.findById(eles.get(i).getRecordId());
//				if (ele != null) {
//					Calendar calendar = Calendar.getInstance();
//			        calendar.clear();
//			        calendar.setTime((ele.getStartTime()));
//			        calendar.add(Calendar.SECOND, -2);
//			        ele.setStartTime(calendar.getTime());
//					logger.info("updateTime:" + ele.getPhoneNum());
//					this.eleRecordeService.updateEleRecord(ele);
//				}
//			}
//			return ResultMessage.getSucceed(null);
//		}
//		return ResultMessage.getFailt("");
//	}
//}
